package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

import com.qy.dao.OrderInfoDao;

import java.util.List;
import java.util.Map;
import java.sql.Types;

import com.qy.db.DbParameter;

import java.util.HashMap;
import java.util.ArrayList;

import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.OrderInfo;
import com.qy.db.BaseDao;

/**
* u_order_info daoImpl实现类的生成
* @author zhangwenlong
* @date Mon Feb 05 15:54:04 GMT+08:00 2018
*/ 
public class OrderInfoDaoImpl extends BaseDao  implements OrderInfoDao {

	@Override
	public boolean createOrderInfo(OrderInfo orderInfo) {
		boolean result = false;
		int id = -1;
		if (orderInfo.beginAdd()) {
			String sql = "insert into u_order_info (id,user_id,fetch_date,fetch_location,fetch_coord,fetch_user_name,fetch_phone,return_date,return_location,return_coord,return_phone,return_user_name,plate_number,vln,order_type,create_date,order_status,pay_id,channel_id,f_driver,s_driver,t_driver,station_id) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, orderInfo.getId()));
			param.put(2, new DbParameter(Types.INTEGER, orderInfo.getUserId()));
			param.put(3, new DbParameter(Types.TIMESTAMP, orderInfo.getFetchDate()));
			param.put(4, new DbParameter(Types.VARCHAR, orderInfo.getFetchLocation()));
			param.put(5, new DbParameter(Types.VARCHAR, orderInfo.getFetchCoord()));
			param.put(6, new DbParameter(Types.VARCHAR, orderInfo.getFetchUserName()));
			param.put(7, new DbParameter(Types.VARCHAR, orderInfo.getFetchPhone()));
			param.put(8, new DbParameter(Types.TIMESTAMP, orderInfo.getReturnDate()));
			param.put(9, new DbParameter(Types.VARCHAR, orderInfo.getReturnLocation()));
			param.put(10, new DbParameter(Types.VARCHAR, orderInfo.getReturnCoord()));
			param.put(11, new DbParameter(Types.VARCHAR, orderInfo.getReturnPhone()));
			param.put(12, new DbParameter(Types.VARCHAR, orderInfo.getReturnUserName()));
			param.put(13, new DbParameter(Types.VARCHAR, orderInfo.getPlateNumber()));
			param.put(14, new DbParameter(Types.VARCHAR, orderInfo.getVln()));
			param.put(15, new DbParameter(Types.INTEGER, orderInfo.getOrderType()));
			param.put(16, new DbParameter(Types.TIMESTAMP, orderInfo.getCreateDate()));
			param.put(17, new DbParameter(Types.INTEGER, orderInfo.getOrderStatus()));
			param.put(18, new DbParameter(Types.INTEGER, orderInfo.getPayId()));
			param.put(19, new DbParameter(Types.INTEGER, orderInfo.getChannelId()));
			param.put(20, new DbParameter(Types.INTEGER, orderInfo.getFDriver()));
			param.put(21, new DbParameter(Types.INTEGER, orderInfo.getSDriver()));
			param.put(22, new DbParameter(Types.INTEGER, orderInfo.getTDriver()));
			param.put(23, new DbParameter(Types.INTEGER, orderInfo.getStationId()));
			id = execLastId(sql, param);
			result = id > -1;
			orderInfo.commitAdd(result);
			orderInfo.setId(id);
		}
		return result;
	}

	@Override
	public List<OrderInfo> getOrderInfoList() {
		String sql = "select id from u_order_info;";
		PreparedStatement pstmt = execQuery(sql, null);
		ResultSet rs = null;
		List<OrderInfo> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<OrderInfo>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultToId(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public OrderInfo getOrderInfoById(int id) {
		String sql = "select * from u_order_info where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		OrderInfo orderInfo = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					orderInfo = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return orderInfo;
	}

	@Override
	public boolean updateOrderInfo(OrderInfo orderInfo) {
		boolean result = false;
		if (orderInfo.beginUpdate()) {
			String sql = "update u_order_info set user_id=?,fetch_date=?,fetch_location=?,fetch_coord=?,fetch_user_name=?,fetch_phone=?,return_date=?,return_location=?,return_coord=?,return_phone=?,return_user_name=?,plate_number=?,vln=?,order_type=?,create_date=?,order_status=?,pay_id=?,channel_id=?,f_driver=?,s_driver=?,t_driver=?,station_id=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, orderInfo.getUserId()));
			param.put(2, new DbParameter(Types.TIMESTAMP, orderInfo.getFetchDate()));
			param.put(3, new DbParameter(Types.VARCHAR, orderInfo.getFetchLocation()));
			param.put(4, new DbParameter(Types.VARCHAR, orderInfo.getFetchCoord()));
			param.put(5, new DbParameter(Types.VARCHAR, orderInfo.getFetchUserName()));
			param.put(6, new DbParameter(Types.VARCHAR, orderInfo.getFetchPhone()));
			param.put(7, new DbParameter(Types.TIMESTAMP, orderInfo.getReturnDate()));
			param.put(8, new DbParameter(Types.VARCHAR, orderInfo.getReturnLocation()));
			param.put(9, new DbParameter(Types.VARCHAR, orderInfo.getReturnCoord()));
			param.put(10, new DbParameter(Types.VARCHAR, orderInfo.getReturnPhone()));
			param.put(11, new DbParameter(Types.VARCHAR, orderInfo.getReturnUserName()));
			param.put(12, new DbParameter(Types.VARCHAR, orderInfo.getPlateNumber()));
			param.put(13, new DbParameter(Types.VARCHAR, orderInfo.getVln()));
			param.put(14, new DbParameter(Types.INTEGER, orderInfo.getOrderType()));
			param.put(15, new DbParameter(Types.TIMESTAMP, orderInfo.getCreateDate()));
			param.put(16, new DbParameter(Types.INTEGER, orderInfo.getOrderStatus()));
			param.put(17, new DbParameter(Types.INTEGER, orderInfo.getPayId()));
			param.put(18, new DbParameter(Types.INTEGER, orderInfo.getChannelId()));
			param.put(19, new DbParameter(Types.INTEGER, orderInfo.getFDriver()));
			param.put(20, new DbParameter(Types.INTEGER, orderInfo.getSDriver()));
			param.put(21, new DbParameter(Types.INTEGER, orderInfo.getTDriver()));
			param.put(22, new DbParameter(Types.INTEGER, orderInfo.getStationId()));
			param.put(23, new DbParameter(Types.INTEGER, orderInfo.getId()));
			result = execNoneQuery(sql, param) > -1;
			orderInfo.commitUpdate(result);
		}
		return result;
	}

	public OrderInfo resultTo(ResultSet rs) throws SQLException {
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setId(rs.getInt("id"));
		orderInfo.setUserId(rs.getInt("user_id"));
		orderInfo.setFetchDate(rs.getTimestamp("fetch_date"));
		orderInfo.setFetchLocation(rs.getString("fetch_location"));
		orderInfo.setFetchCoord(rs.getString("fetch_coord"));
		orderInfo.setFetchUserName(rs.getString("fetch_user_name"));
		orderInfo.setFetchPhone(rs.getString("fetch_phone"));
		orderInfo.setReturnDate(rs.getTimestamp("return_date"));
		orderInfo.setReturnLocation(rs.getString("return_location"));
		orderInfo.setReturnCoord(rs.getString("return_coord"));
		orderInfo.setReturnPhone(rs.getString("return_phone"));
		orderInfo.setReturnUserName(rs.getString("return_user_name"));
		orderInfo.setPlateNumber(rs.getString("plate_number"));
		orderInfo.setVln(rs.getString("vln"));
		orderInfo.setOrderType(rs.getInt("order_type"));
		orderInfo.setCreateDate(rs.getTimestamp("create_date"));
		orderInfo.setOrderStatus(rs.getInt("order_status"));
		orderInfo.setPayId(rs.getInt("pay_id"));
		orderInfo.setChannelId(rs.getInt("channel_id"));
		orderInfo.setFDriver(rs.getInt("f_driver"));
		orderInfo.setSDriver(rs.getInt("s_driver"));
		orderInfo.setTDriver(rs.getInt("t_driver"));
		orderInfo.setStationId(rs.getInt("station_id"));
		orderInfo.setOp(Option.None);
		return orderInfo;
	}

	public OrderInfo resultToId(ResultSet rs) throws SQLException {
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setId(rs.getInt("id"));
		orderInfo.setOp(Option.None);
		return orderInfo;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}
	
	@Override
	public List<OrderInfo> getOrderInfoList(int userId) {
		String sql = "select id from u_order_info WHERE user_id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, userId));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		List<OrderInfo> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<OrderInfo>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultToId(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public List<OrderInfo> getOrderInfoListByStatus(List<Integer> status) {
	    StringBuffer sqlB = new StringBuffer();
	    Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
	    param.put(1, new DbParameter(Types.INTEGER, status));
	    sqlB.append("select id from u_order_info WHERE order_status=? ");
	    for(int i = 0;i<status.size()-1;i++){
		sqlB.append("OR  order_status=?");
		param.put(i+2, new DbParameter(Types.INTEGER, status));
	    }
	    sqlB.append(";");
	    String sql =sqlB.toString();
	    PreparedStatement pstmt = execQuery(sql, param);
	    ResultSet rs = null;
	    List<OrderInfo> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<OrderInfo>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultToId(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

}

